package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBagItem;

import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBagDaoImpl extends TempDataObjectDao<UserBagItem> {

	/**
	 * 查询用户的所有道具
	 */
	public Map<Integer, UserBagItem> getUserBagItemMap(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, UserBagItem> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_u_bag where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserBagItem data = parse(rs);
				dataMap.put(data.getGoodsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	/**
	 * 查询用户的所有道具
	 */
	public UserBagItem getUserBagItem(long userId, int goodsId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_bag where userId=? and goodsId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setInt(2, goodsId);
			rs = call.executeQuery();
			if (rs.next()) {
				return parse(rs);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserBagItem data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_bag(userId,num,goodsId,modifyTime) VALUES(?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setLong(i++, data.getNum());
			call.setInt(i++, data.getGoodsId());
			call.setLong(i++, data.getModifyTime());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserBagItem data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_bag SET num=?,modifyTime=? WHERE goodsId=? and userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getNum());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getGoodsId());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserBagItem parseData(ResultSet rs) throws SQLException {
		UserBagItem data = new UserBagItem();
		data.setUserId(rs.getLong("userId"));
		data.setGoodsId(rs.getInt("goodsId"));
		data.setNum(rs.getLong("num"));
		data.setModifyTime(rs.getLong("modifyTime"));
		return data;
	}
}
